const fs = require('fs')
const path = require('path')

// 需要处理的文件列表
const listFiles = [
  'src/views/dataManagement/landManagement/landExpropriation/list.vue',
  'src/views/dataManagement/landManagement/landSupply/list.vue',
  'src/views/dataManagement/leaderApprove/list.vue',
  'src/views/dataManagement/projectManagement/constructProjectInfo/list.vue',
  'src/views/dataManagement/projectManagement/fixedTargetMap/list.vue',
  'src/views/dataManagement/projectManagement/investProjectInfo/list.vue',
  'src/views/dataManagement/projectManagement/investYearPlan/list.vue',
  'src/views/dataManagement/projectManagement/sectionManagement/list.vue',
  'src/views/dataManagement/technologyManagement/centerPlatform/list.vue',
  'src/views/dataManagement/technologyManagement/civilAviationZone/list.vue',
  'src/views/dataManagement/technologyManagement/gatherTalents/list.vue',
  'src/views/dataManagement/technologyManagement/incubateEnterprises/list.vue',
  'src/views/dataManagement/technologyManagement/laboratoryManagement/list.vue',
  'src/views/dataManagement/technologyManagement/scientificInstitutions/list.vue'
]

// 处理每个文件
listFiles.forEach((filePath) => {
  try {
    let content = fs.readFileSync(filePath, 'utf8')

    // 1. 添加mixin import
    if (!content.includes('listKeepAliveMixin')) {
      content = content.replace(
        /import.*from.*@\/api\/dataManagement.*\n/,
        (match) =>
          match +
          "import listKeepAliveMixin from '@/mixins/listKeepAliveMixin'\n"
      )

      // 2. 添加mixin到组件
      content = content.replace(
        /export default \{\s*name:/,
        'export default {\n  name:'
      )

      content = content.replace(
        /(export default \{\s*name:\s*'[^']+',)/,
        '$1\n  mixins: [listKeepAliveMixin],'
      )
    }

    // 3. 移除shouldResetFilters from data
    content = content.replace(
      /,\s*shouldResetFilters:\s*false/g,
      ''
    )

    // 4. 移除原有的生命周期钩子
    content = content.replace(
      /beforeRouteEnter\(to, from, next\) \{[\s\S]*?\},\s*/g,
      ''
    )

    content = content.replace(
      /activated\(\) \{[\s\S]*?\},\s*/g,
      ''
    )

    content = content.replace(
      /deactivated\(\) \{[\s\S]*?\},\s*/g,
      ''
    )

    // 写回文件
    fs.writeFileSync(filePath, content, 'utf8')
    console.log(`✅ 已处理: ${filePath}`)
  } catch (error) {
    console.error(`❌ 处理失败 ${filePath}:`, error.message)
  }
})
